package lego.jbv;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.*;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import lejos.pc.comm.*;
import lejos.util.Delay;

import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.ArrayBlockingQueue;

public class main extends JFrame implements ActionListener {

	private JButton connectButton = new JButton("Connect");
	private JButton stopButton = new JButton("Emergency stop");
	private JButton linkButton = new JButton("Link button");

	private JLabel status = new JLabel("Status: ");
	private JLabel statusValue = new JLabel("");

	private CommunicateThread com;
	private ArrayBlockingQueue<Integer[]> queue;
	private Integer[] powers = { 0, 0 };

	private boolean[] keys = { false, false, false, false };

	public main() {
		queue = new ArrayBlockingQueue<Integer[]>(2);
		com = new CommunicateThread(queue);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Remote controlled car");
		setSize(500, 300);

		// holds connect and stop button
		JPanel p2 = new JPanel();
		p2.add(connectButton);
		connectButton.addActionListener(this);
		p2.add(stopButton);
		stopButton.addActionListener(this);
		p2.add(linkButton);

		JPanel p3 = new JPanel();
		p3.add(status);
		p3.add(statusValue);

		// North area of the frame
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(8, 1));
		panel.add(p2);
		panel.add(p3);
		add(panel, BorderLayout.NORTH);

		linkButton.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_LEFT:
					keys[0] = false;
					break;

				case KeyEvent.VK_RIGHT:
					keys[2] = false;
					break;

				case KeyEvent.VK_UP:
					keys[1] = false;
					break;

				case KeyEvent.VK_DOWN:
					keys[3] = false;
					break;
					
				case KeyEvent.VK_2:
					powers[0] = 0;
					powers[1] = 0;
					break;
					
				case KeyEvent.VK_DELETE:
					powers[0] = 0;
					powers[1] = 0;
					break;

				default:
					break;
				}

				updateCommand();
			}

			@Override
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_LEFT:
					keys[0] = true;
					statusValue.setText("LEFT");
					break;

				case KeyEvent.VK_RIGHT:
					keys[2] = true;
					statusValue.setText("RIGHT");
					break;

				case KeyEvent.VK_UP:
					keys[1] = true;
					statusValue.setText("UP");
					break;

				case KeyEvent.VK_DOWN:
					keys[3] = true;
					statusValue.setText("DOWN");
					break;

				case KeyEvent.VK_ESCAPE:
					powers[0] = 404;
					powers[1] = 404;
					queue.add(powers);
					break;
					
				case KeyEvent.VK_2:
					powers[0] = 200;
					powers[1] = 200;
					queue.add(powers);
					break;
					
				case KeyEvent.VK_DELETE:
					statusValue.setText("RESETED");
					powers[0] = 300;
					powers[1] = 300;
					queue.add(powers);
					break;

				default:
					statusValue.setText(":(");
					break;
				}

				updateCommand();
			}
		});
	}

	private void updateCommand() {
		if (powers[0] != 404 && powers[0] != 200 && powers[0] != 300) {
			// Without gearing
			/*
			 * // Forward if (!keys[0] && keys[1] && !keys[2] && !keys[3]) {
			 * powers[0] = 100; powers[1] = 100; }
			 * 
			 * // Backward else if (!keys[0] && !keys[1] && !keys[2] && keys[3])
			 * { powers[0] = -100; powers[1] = -100; }
			 * 
			 * // Left else if (keys[0] && !keys[1] && !keys[2] && !keys[3]) {
			 * powers[0] = -55; powers[1] = 100; }
			 * 
			 * // Right else if (!keys[0] && !keys[1] && keys[2] && !keys[3]) {
			 * powers[0] = 100; powers[1] = -55; }
			 * 
			 * // Forward + Left else if (keys[0] && keys[1] && !keys[2] &&
			 * !keys[3]) { powers[0] = 75; powers[1] = 100; }
			 * 
			 * // Forward + Right else if (!keys[0] && keys[1] && keys[2] &&
			 * !keys[3]) { powers[0] = 100; powers[1] = 75; }
			 * 
			 * // Backward + Left else if (keys[0] && !keys[1] && !keys[2] &&
			 * keys[3]) { powers[0] = -100; powers[1] = -75; }
			 * 
			 * // Backward + Right else if (!keys[0] && !keys[1] && keys[2] &&
			 * keys[3]) { powers[0] = -75; powers[1] = -100; }
			 */

			//With gearing
			// Forward
			if (!keys[0] && keys[1] && !keys[2] && !keys[3]) {
				powers[0] = -100;
				powers[1] = -100;
			}

			// Backward
			else if (!keys[0] && !keys[1] && !keys[2] && keys[3]) {
				powers[0] = 100;
				powers[1] = 100;
			}

			// Left
			else if (keys[0] && !keys[1] && !keys[2] && !keys[3]) {
				powers[0] = 55;
				powers[1] = -100;
			}

			// Right
			else if (!keys[0] && !keys[1] && keys[2] && !keys[3]) {
				powers[0] = -100;
				powers[1] = 55;
			}

			// Forward + Left
			else if (keys[0] && keys[1] && !keys[2] && !keys[3]) {
				powers[0] = -85;
				powers[1] = -100;
			}

			// Forward + Right
			else if (!keys[0] && keys[1] && keys[2] && !keys[3]) {
				powers[0] = -100;
				powers[1] = -85;
			}

			// Backward + Left
			else if (keys[0] && !keys[1] && !keys[2] && keys[3]) {
				powers[0] = 100;
				powers[1] = 85;
			}

			// Backward + Right
			else if (!keys[0] && !keys[1] && keys[2] && keys[3]) {
				powers[0] = 85;
				powers[1] = 100;
			}

			else {
				powers[0] = 0;
				powers[1] = 0;
			}

			queue.add(powers);
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == connectButton) {
			com.start();
		}

		else if (e.getSource() == stopButton) {
		}
	}

	public static void main(String[] args) {
		main frame = new main();
		frame.setVisible(true);
	}
}